﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WZ.Stock.Data;

namespace WZ.ST.Site.Controllers
{
    public class AnnController : Controller
    {
        //
        // GET: /Ann/

        public ActionResult Index()
        {
            ViewBag.Keywords = new dic_AnnouncementKeywordDAL().SelectTo().ToList().FindAll(a => a.TypeId == 0);
            string chb_keyword = Common.RequestHelper.GetString("chb_keyword");
            string txt_code = ViewBag.txt_code = Common.RequestHelper.GetString("txt_code").Trim();
            string txt_searchValue = ViewBag.txt_searchValue = Common.RequestHelper.GetString("txt_searchValue").Trim();
            int days = ViewBag.days = Common.RequestHelper.GetInt("txt_days", 1);
            int typeid = ViewBag.typeid = Common.RequestHelper.GetInt("txt_typeid", -1);
            DateTime beginTime = DateTime.Now.AddDays(-days).Date;
            DateTime endTime = DateTime.Now.Date.AddDays(1);

            ViewBag.chb_keyword = chb_keyword.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
            ViewBag.AnnList = new AnnouncementDAL().SearchAnnoumentList(
                chb_keyword.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                , typeid
                , txt_code
                , beginTime
                , endTime
                , txt_searchValue);

            return View();
        }
        public ActionResult GetAnnouncement()
        {
            int pageNum = 1;
            bool isExists = false;
            StringBuilder sb = new StringBuilder();
            int totalRecord = 0; //总共筛选的数据
            int totalSuccessRecord = 0;
            int days = Common.RequestHelper.GetInt("d", 1);
            while (pageNum < 200)
            {
                int pageNumExistCount = 0;//当前页有效数据
                string urlParams = string.Format("stock=&searchkey=&plate=&category=&trade=&column=szse&columnTitle=%E5%8E%86%E5%8F%B2%E5%85%AC%E5%91%8A%E6%9F%A5%E8%AF%A2&pageNum={0}&pageSize=30&tabName=fulltext&sortName=&sortType=&limit=&showTitle=&seDate=", pageNum);
                WZ.WebSpider.AnnouncementSpider spider = new WZ.WebSpider.AnnouncementSpider();
                List<WZ.Entity.Announcement> market = spider.GetWebData(urlParams);
                foreach (var item in market)
                {
                    try
                    {
                        isExists = spider.Save(item);
                        totalRecord++;
                        if (item.Id > 0)
                        {
                            sb.AppendLine(string.Format("公司名：{0}，代码：{1}，数据库ID：{2}", item.secName, item.secCode, item.Id));
                            pageNumExistCount++;
                            totalSuccessRecord++;
                        }
                        // Thread.Sleep(200);
                    }
                    catch
                    {
                        continue;
                    }


                }
                if (pageNumExistCount == 0)
                {
                    break;
                }
                pageNum++;
            }
            return Json(new { totalRecord = totalRecord, log = sb.ToString(), totalSuccessRecord = totalSuccessRecord });
        }

    }
}
